﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.ApplicationSettings;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows8Accelerator.Core.Xaml.Helpers;

namespace Windows8Accelerator.Core.Xaml.Controls
{
    public enum SuperSettingsFlyoutWidth
    {
        Wide = 646,
        Narrow = 346
    }

    public static class SuperSettingsFlyoutHelper
    {
        public static SuperSettingsFlyout CreateSuperSettingsFlyout(this Panel hostPanel, string headerText, string settingsTitle, SuperSettingsFlyoutWidth width, Brush background, UIElement content, Brush headerBackground = null)
        {
            var superSettingsFlyout = new SuperSettingsFlyout(true)
            {
                HeaderText = headerText,
                SettingsTitle = settingsTitle,
                Width = width,
                Background = background,
                HeaderBackground = headerBackground,
                Content = content
            };
            hostPanel.Children.Add(superSettingsFlyout);
            return superSettingsFlyout;
        }
    }


    public class SuperSettingsFlyout : SuperFlyout
    {
        public SuperSettingsFlyout(bool removeFromParentOnHide)
            : base(removeFromParentOnHide, SuperFlyoutPosition.Right)
        {
            Loaded += SuperSettingsFlyout_Loaded;
        }

        public SuperSettingsFlyout()
        {
            Loaded += SuperSettingsFlyout_Loaded;
        }

        void SuperSettingsFlyout_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            var page = this.GetParentOf<Page>();
            if (page != null)
            {
                SettingsContractHelper.AddSettingsCommand(SettingsTitle, page, RegisterInAllPages, () =>
                {
                    Show();
                });
            }
        }


        public override void Show()
        {
            base.Width = (int)Width;
 	        base.Show();
        }


        public string SettingsTitle
        {
            get { return (string)GetValue(SettingsTitleProperty); }
            set { SetValue(SettingsTitleProperty, value); }
        }
        public static readonly DependencyProperty SettingsTitleProperty =
            DependencyProperty.Register("SettingsTitle", typeof(string), typeof(SuperSettingsFlyout), new PropertyMetadata("Settings"));

        public new SuperSettingsFlyoutWidth Width
        {
            get { return (SuperSettingsFlyoutWidth)GetValue(WidthProperty); }
            set { SetValue(WidthProperty, value); }
        }
        public new static readonly DependencyProperty WidthProperty =
            DependencyProperty.Register("Width", typeof(SuperSettingsFlyoutWidth), typeof(SuperSettingsFlyout), new PropertyMetadata(SuperSettingsFlyoutWidth.Narrow));

        public bool RegisterInAllPages
        {
            get { return (bool)GetValue(RegisterInAllPagesProperty); }
            set { SetValue(RegisterInAllPagesProperty, value); }
        }
        public static readonly DependencyProperty RegisterInAllPagesProperty =
            DependencyProperty.Register("RegisterInAllPages", typeof(bool), typeof(SuperSettingsFlyout), new PropertyMetadata(false));


    }
}
